$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'exam/simulationscore/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden:true},
			{ label: '单位编码，同部门中的dept_no', name: 'deptNo', index: 'dept_no', width: 80 , hidden:true}, 			
			{ label: '考试id', name: 'examinationId', index: 'examination_id', width: 80 , hidden:true},
			{ label: '试卷ID', name: 'paperId', index: 'paper_id', width: 80 , hidden:true},
			{ label: '试卷名称', name: 'paperName', index: 'paper_name', width: 80 },
			{ label: '考生名字', name: 'createName', index: 'createName', width: 80 },
			{ label: '随机答题试卷id', name: 'paperProduceId', index: 'paper_produce_id', width: 80, hidden:true},
			{ label: '总分数', name: 'score', index: 'score', width: 80 },
			{ label: '单项选择题分数', name: 'singleScore', index: 'single_score', width: 80 , hidden:true}, 			
			{ label: '多项选择题分数', name: 'doubleScore', index: 'double_score', width: 80 , hidden:true}, 			
			{ label: '判断题分数', name: 'judgmentScore', index: 'judgment_score', width: 80 , hidden:true},			
			{ label: '正确答案', name: 'trueAnswer', index: 'true_answer', width: 80 , hidden:true}, 			
			{ label: '填写答案', name: 'myAnswer', index: 'my_answer', width: 80 , hidden:true}, 			
		
			{ label: '考试用时(秒)', name: 'timeLong', index: 'time_long', width: 80 , hidden:true}, 			
			{ label: '交卷时间', name: 'createTime', index: 'create_time', width: 80 , formatter:function(value,options,row){
                return new Date(value).Format('yyyy-MM-dd HH:mm:ss');}}, 		
			{ label: '用户', name: 'userId', index: 'user_id', width: 80 , hidden:true}, 			
			{ label: '部门id', name: 'deptId', index: 'dept_id', width: 80 , hidden:true},
			{ label: '部门名称', name: 'deptName', index: 'dept_name', width: 80}
			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        //multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "simulationScoreList.list",
            page: "simulationScoreList.pageNum",
            total: "simulationScoreList.pages",
            records: "simulationScoreList.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
	vm.getDept();
});
var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "deptId",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	}
};
var ztree;
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		queryParam:{
		    paperName : null,
		    createName : null,
			deptId:null,
			deptName:null,
			deptNo:null,
          },
		simulationScore: {}
	},
	methods: {
		getDept: function(){
		//加载部门树
		$.get(baseURL + "sys/dept/listByUser", function(r){
			ztree = $.fn.zTree.init($("#deptTree"), setting, r);
			var node = ztree.getNodeByParam("deptId", vm.queryParam.deptId);
			if(node != null){
				ztree.selectNode(node);
				//修改时，不能回显.下边的解决了.
				//vm.workDaily.deptName = node.name;
				Vue.set(vm.queryParam, 'deptName', node.name)
				Vue.set(vm.queryParam, 'deptNo', node.deptNo)
			}
		})
	},
	deptTree: function(){
		layer.open({
			type: 1,
			offset: '50px',
			skin: 'layui-layer-molv',
			title: "选择部门",
			area: ['300px', '450px'],
			shade: 0,
			shadeClose: false,
			content: jQuery("#deptLayer"),
			btn: ['确定', '取消'],
			btn1: function (index) {
				var node = ztree.getSelectedNodes();
				//选择上级部门
				vm.queryParam.deptId = node[0].deptId;
				vm.queryParam.deptName = node[0].name;
				vm.queryParam.deptNo = node[0].deptNo;

				layer.close(index);
			}
		});
	},
		query: function () {
			vm.reload();
		},
		reset : function(){
            vm.queryParam.paperName = null;
            vm.queryParam.createName = null;
			vm.queryParam.deptNo=null
			vm.queryParam.deptName=null
			vm.queryParam.deptId=null
            vm.query();
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
			    postData:{
			        'paperName': vm.queryParam.paperName,
	                'createName': vm.queryParam.createName,
	                'deptNo': vm.queryParam.deptNo
			    },
			    page:page
            }).trigger("reloadGrid");

		},
		imp:function(){
			if(vm.queryParam.deptNo==null){
				vm.queryParam.deptNo=''
			}
			if(vm.queryParam.createName==null){
				vm.queryParam.createName=''
			}
			if(vm.queryParam.paperName==null){
				vm.queryParam.paperName=''
			}
			var url = "exam/simulationscore/imp?deptNo="+vm.queryParam.deptNo
				+"&paperName="+vm.queryParam.paperName
				+"&createName="+vm.queryParam.createName;
			
			window.location.href = baseURL +url;
//			$.ajax({
//				type: "POST",
//			    url: baseURL + url,
//                contentType: "application/json",
//			    data: JSON.stringify(vm.section),
//			    success: function(r){
//			    	if(r.code === 0){
//						alert('操作成功');
//					}else{
//						alert(r.msg);
//					}
//				}
//			});
			
		},
	}
});

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}